package com.vhly.appspot;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: vhly
 * Date: 2009-11-11
 * Time: 12:46:44
 *
 * @author vhly[FR]
 */
public final class ResponseData implements Serializable {

    private static final long serialVersionUID = 958809372234L;

    public int stateCode;
    public int contentLength;
    public String contentType;
    public String contentEncoding;
    public byte[] data;

    public void dealloc() {
        data = null;
        contentEncoding = null;
        contentType = null;
    }

    public String getDescription() {
        StringBuffer sb = new StringBuffer("RespData:");
        sb.append("statcode=").append(stateCode)
                .append(" len=").append(contentLength)
                .append(" type=").append(contentType)
                .append(" enc=").append(contentEncoding);
        return sb.toString();
    }

    public void readObject(ObjectInputStream in) throws IOException {
        stateCode = in.readInt();
        contentLength = in.readInt();
        contentType = in.readUTF();
        contentEncoding = in.readUTF();
        int len = in.readInt();
        if (len > 0) {
            data = new byte[len];
            in.read(data, 0, len);
        }
    }

    public void writeObject(ObjectOutputStream out) throws IOException {
        out.writeInt(stateCode);
        out.writeInt(contentLength);
        out.writeUTF(contentType);
        out.writeUTF(contentEncoding);
        int len = 0;
        if (data != null) {
            len = data.length;
            out.writeInt(len);
            out.write(data);
        } else {
            out.writeInt(len);
        }
    }
}
